package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.southwind.entity.Clean;
import com.southwind.entity.Dormitory;
import com.southwind.form.SearchForm;
import com.southwind.service.BuildingService;
import com.southwind.service.CleanService;
import com.southwind.service.DormitoryService;
import com.southwind.util.ResultVOUtil;
import com.southwind.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/clean")
public class CleanController {
    @Autowired
    private CleanService cleanService;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private DormitoryService dormitoryService;

    @GetMapping("/list/{page}/{size}")
    public ResultVO list(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        return ResultVOUtil.success(this.cleanService.list(page, size));
    }

    @GetMapping("/search")
    public ResultVO search(SearchForm searchForm){
        return ResultVOUtil.success(this.cleanService.search(searchForm));
    }

    @GetMapping("/buildingList")
    public ResultVO buildingList(){
        return ResultVOUtil.success(this.buildingService.list());
    }

    @GetMapping("/findDormitoryByBuildingId/{id}")
    public ResultVO findDormitoryByBuildingId(@PathVariable("id") Integer id){
        QueryWrapper<Dormitory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("building_id", id);
        List<Dormitory> dormitoryList = this.dormitoryService.list(queryWrapper);
        return ResultVOUtil.success(dormitoryList);
    }

    @PostMapping("/save")
    public ResultVO save(@RequestBody Clean clean){
        String date = clean.getDate();
        date = date.substring(0, 10);
        clean.setDate(date);
        boolean save = this.cleanService.save(clean);
        if(!save) return ResultVOUtil.fail();
        return ResultVOUtil.success(null);
    }
}
